package com.book.service.impl;

import com.book.mapper.BookDao;
import com.book.mapper.ChapterDao;
import com.book.pojo.Author;
import com.book.mapper.AuthorDao;
import com.book.pojo.Book;
import com.book.pojo.Chapter;
import com.book.pojo.ChapterContent;
import com.book.service.AuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.vo.AuthorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cc
 * @since 2023-10-11 21:57:48
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorDao, Author> implements AuthorService {

    @Autowired
    private AuthorDao authorDao;
    @Autowired
    private BookDao bookDao;
    @Autowired
    private ChapterDao chapterDao;
    @Override
    public int getBookNumByAuthorId(Long id) {
        int num =  authorDao.getBookNumByAuthorId(id);
        return num;
    }

    @Override
    public List<Author> getAuthorById(Long id) {

        return (List<Author>) authorDao.selectById(id);
    }

    @Override
    public AuthorVo getAuthorCenterById(Long id) {
        //查询所有信息
        Author author = authorDao.selectById(id);

        //注册天数
        LocalDate creatTime = author.getCreatTime();
        LocalDate now = LocalDate.now();
        Period period = Period.between(creatTime, now);
        int days = period.getDays();

        //根据作者id找到对应的作品集合
        Integer bookNumber=0;
        List<Book> books= bookDao.getBooksByAuthorId(id);
        for (Book book : books) {
            Long bookId = book.getId();
            //一个作品总字数
             bookNumber = chapterDao.getBookNumber(bookId);
        }

        //转换
        AuthorVo authorVo = new AuthorVo();
        BeanUtils.copyProperties(author,authorVo);
        //存入
        authorVo.setDays(days);
        authorVo.setBooks(books);
        authorVo.setBookNumber(bookNumber);
        return authorVo;
    }

    @Override
    public int updateAuthor(Author author) {
        return authorDao.updateById(author);
    }



}
//书的每章节总字数
//Chapter chapter = new Chapter();
//        book.setBookCount();